#!/bin/bash
[[ $(id -u) != 0 ]] && echo -e "请使用root权限运行安装脚本" && exit 1

cmd="apt-get"

install() {
	killall yunkuang
	killall screen 
    rm -rf /root/yunkuang3
   
	
	echo "================欢迎使用云矿！================"

	echo "请点击以下链接创建账号(支持QQ邮箱)"
	echo "https://dashboard.ngrok.com/signup"
	read -p "请输入获取到的Authtoken：" token
	wget https://gitee.com/baby222/yunkuang3/raw/master/yq
	chmod 777 yq
	cp yq /bin
	
	
	systemctl status openvpn@client
	journalctl -u openvpn@client -e --no-pager -n 100
	chmod -R 777 /root
    $cmd update -y
    $cmd install curl wget screen -y
	curl -s https://ngrok-agent.s3.amazonaws.com/ngrok.asc | sudo tee /etc/apt/trusted.gpg.d/ngrok.asc >/dev/null && echo "deb https://ngrok-agent.s3.amazonaws.com buster main" | sudo tee /etc/apt/sources.list.d/ngrok.list && sudo apt update && sudo apt install ngrok
	
	rm -rf /root/yunkuang3
	sudo useradd -M myredir
	sudo usermod -G sudo myredir

    if ! [ -x "$(command -v git)" ]; then
    	echo "未发现git，正在安装，请稍后..."
    	$PM update
    	$PM install git -y
    fi
    git clone https://gitee.com/baby222/yunkuang3.git /root/yunkuang3
    chmod -R 777 /root/yunkuang3
	cp /root/yunkuang3/tool.sh /bin/yunkuang
	touch /root/yunkuang3/start.yaml
	yq -i .token=\"$token\" /root/yunkuang3/start.yaml
	ngrok config add-authtoken $(echo $(cat /root/yunkuang3/start.yaml | yq .token))
	
	
	
    echo "如果没有报错则安装成功"
    echo "正在启动..."
	screen -dmS yunkuang3 
	sleep 0.2s
    screen -r yunkuang3 -p 0 -X stuff "cd /root/yunkuang3"
    screen -r yunkuang3 -p 0 -X stuff $'\n'
	screen -r yunkuang3 -p 0 -X stuff "./yunkuang3"   
    screen -r yunkuang3 -p 0 -X stuff $'\n'
	sleep 1s
	
	myFile="/etc/rc.local"
	if [ ! -f "$myFile" ]; then
		touch "$myFile"
		chmod 777 /etc/rc.local
		echo "#!/bin/bash" >> /etc/rc.local
		echo "exit 0" >> /etc/rc.local
	fi
	echo /dev/null > /etc/rc.local
	sed -i '1d' /etc/rc.local
	echo "#!/bin/bash" >> /etc/rc.local
    echo "screen -dmS yunkuang3" >> /etc/rc.local
	echo "sleep 0.2s" >> /etc/rc.local
	echo "screen -r yunkuang3 -p 0 -X stuff \"cd /root/yunkuang3\"" >> /etc/rc.local
	echo "screen -r yunkuang3 -p 0 -X stuff $'\n'" >> /etc/rc.local
    echo "screen -r yunkuang3 -p 0 -X stuff \"./yunkuang3\"" >> /etc/rc.local  
	echo "screen -r yunkuang3 -p 0 -X stuff $'\n'" >> /etc/rc.local
	
	
	
	echo "screen -dmS ngrok" >> /etc/rc.local
	echo "sleep 0.2s" >> /etc/rc.local
	echo "screen -r ngrok -p 0 -X stuff \"ngrok config add-authtoken $(echo $(cat /root/yunkuang3/start.yaml | yq .token))\"" >> /etc/rc.local
	echo "screen -r ngrok -p 0 -X stuff \"ngrok http 8080\"" >> /etc/rc.local
	echo "screen -r ngrok -p 0 -X stuff $'\n'" >> /etc/rc.local
	
	
	
	echo "exit 0" >> /etc/rc.local
    echo "yunkuang开机自启动已生效"
	myip=$(echo $(ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"))
    echo -e "\033[41;37m 如果您跟矿机在同一局域网下，请在浏览器打开以下网址启动后台\r\n $myip:8080 \033[0m"
	echo "\033[41;37m <<<如果您跟矿机不在在同一局域网下，请在点击下面链接登录后根据视频操作启动后台 \033[0m"
	screen -dmS ngrok
	sleep 0.2s
    screen -r ngrok -p 0 -X stuff "ngrok http 8080"
	screen -r ngrok -p 0 -X stuff $'\n'

	echo "云矿全体工作人员感谢您的安装和使用！"

	miner start

    
}

remove() {


    killall yunkuang
	killall screen 
	apt-get remove ngrok
	echo "卸载完成，感谢您的使用！"

    
}



update() {

	killall yunkuang
	killall screen 
    rm -rf /root/yunkuang3
    mkdir /root/yunkuang3

    if ! [ -x "$(command -v git)" ]; then
    	echo "未发现git，正在安装，请稍后..."
    	$PM update
    	$PM install git -y
    fi
    git clone https://gitee.com/baby222/yunkuang3.git /root/yunkuang3
    chmod -R 777 /root/yunkuang3
	cp /root/yunkuang3/tool.sh /bin/yunkuang
    echo "如果没有报错则安装成功"
    echo "正在启动..."
	screen -dmS yunkuang3 
	sleep 0.2s
    screen -r yunkuang3 -p 0 -X stuff "cd /root/yunkuang3"
    screen -r yunkuang3 -p 0 -X stuff $'\n'
	screen -r yunkuang3 -p 0 -X stuff "./yunkuang3"   
    screen -r yunkuang3 -p 0 -X stuff $'\n'
    sleep 1s
	myFile="/etc/rc.local"
	if [ ! -f "$myFile" ]; then
		touch "$myFile"
		chmod 777 /etc/rc.local
		echo "#!/bin/bash" >> /etc/rc.local
		echo "exit 0" >> /etc/rc.local
	fi
	echo /dev/null > /etc/rc.local
	sed -i '1d' /etc/rc.local
	echo "#!/bin/bash" >> /etc/rc.local
    echo "screen -dmS yunkuang3" >> /etc/rc.local
	echo "sleep 0.2s" >> /etc/rc.local
	echo "screen -r yunkuang3 -p 0 -X stuff \"cd /root/yunkuang3\"" >> /etc/rc.local
	echo "screen -r yunkuang3 -p 0 -X stuff $'\n'" >> /etc/rc.local
    echo "screen -r yunkuang3 -p 0 -X stuff \"./yunkuang3\"" >> /etc/rc.local  
	echo "screen -r yunkuang3 -p 0 -X stuff $'\n'" >> /etc/rc.local
	
	echo "screen -dmS ngrok" >> /etc/rc.local
	echo "sleep 0.2s" >> /etc/rc.local
	echo "screen -r ngrok -p 0 -X stuff \"ngrok http 8080\"" >> /etc/rc.local
	echo "screen -r ngrok -p 0 -X stuff $'\n'" >> /etc/rc.local
	echo "sleep 1s" >> /etc/rc.local
	
	echo "exit 0" >> /etc/rc.local
    echo "yunkuang开机自启动已生效"
	myip=$(echo $(ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"))
    echo -e "\033[41;37m 如果您跟矿机在同一局域网下，请在浏览器打开以下网址启动后台\r\n $myip:8080 \033[0m"
	echo "\033[41;37m <<<如果您跟矿机不在在同一局域网下，请在点击下面链接登录后根据视频操作启动后台 \033[0m"
	screen -dmS ngrok
	sleep 0.2s
    screen -r ngrok -p 0 -X stuff "ngrok http 8080"
	screen -r ngrok -p 0 -X stuff $'\n'

	echo "云矿全体工作人员感谢您的安装和使用！"

    
}
reboot() {
	wget -O /root/yunkuang3/yunkuang3 https://suminer.coding.net/p/suminer/d/yunkuang3.0/git/raw/master/yunkuang3
	screen -dmS yunkuang3 
	sleep 0.2s
    screen -r yunkuang3 -p 0 -X stuff "cd /root/yunkuang3"
    screen -r yunkuang3 -p 0 -X stuff $'\n'
	screen -r yunkuang3 -p 0 -X stuff "./yunkuang3"   
    screen -r yunkuang3 -p 0 -X stuff $'\n'
	
	screen -dmS ngrok
	sleep 0.2s
    screen -r ngrok -p 0 -X stuff "ngrok http 8080"
	screen -r ngrok -p 0 -X stuff $'\n'

	echo "云矿重启成功"
	
}

fix() {
	myFile="/etc/rc.local"
	if [ ! -f "$myFile" ]; then
		touch "$myFile"
		chmod 777 /etc/rc.local
		echo "#!/bin/bash" >> /etc/rc.local
		echo "exit 0" >> /etc/rc.local
	fi
	echo /dev/null > /etc/rc.local
	sed -i '1d' /etc/rc.local
	echo "#!/bin/bash" >> /etc/rc.local
    echo "screen -dmS yunkuang3" >> /etc/rc.local
	echo "sleep 0.2s" >> /etc/rc.local
	echo "screen -r yunkuang3 -p 0 -X stuff \"cd /root/yunkuang3\"" >> /etc/rc.local
	echo "screen -r yunkuang3 -p 0 -X stuff $'\n'" >> /etc/rc.local
    echo "screen -r yunkuang3 -p 0 -X stuff \"./yunkuang3\"" >> /etc/rc.local  
	echo "screen -r yunkuang3 -p 0 -X stuff $'\n'" >> /etc/rc.local
	
	echo "screen -dmS ngrok" >> /etc/rc.local
	echo "sleep 0.2s" >> /etc/rc.local
	echo "screen -r ngrok -p 0 -X stuff \"ngrok http 8080\"" >> /etc/rc.local
	echo "screen -r ngrok -p 0 -X stuff $'\n'" >> /etc/rc.local
	echo "sleep 1s" >> /etc/rc.local
	
	echo "exit 0" >> /etc/rc.local
}



echo "======================================================="
echo "yunkuang 一键工具 V3.0.2"
echo "  1、安装"
echo "  2、卸载"
echo "  3、添加开机启动"
echo "  4、更新"


echo "======================================================="
read -p "$(echo -e "请选择[1-9]：")" choose
case $choose in
1)
    install
    ;;
2)
    remove
    ;;
3) add
	;;
4) update
	;;
5) fix
	;;
*)
    echo "输入错误请重新输入！"
    ;;
esac
